/*
 *  Copyright © 2014 CIB Co.Ltd
 *  * All right reserved
 *  * Auther:jin xingwang
 *
 */

package com.wsd.scheduler;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author 靳兴旺
 */
@Component
public class TimerRunner implements ApplicationRunner {

    @Autowired
    private Scheduler scheduler;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        scheduler.clear();
        buildCreditCheck();
        buildOverdueCheck();
//        buildMakeLoan();
    }

    /**
     * 创建定时授信任务
     * @throws Exception
     */
    public void buildCreditCheck() throws Exception{
        String creditCheck = "creditCheck";
        String checkGroup = "checkGroup";
        if (scheduler.checkExists(JobKey.jobKey(creditCheck, checkGroup))) {
            return;
        }
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.dailyAtHourAndMinute(2,0);
        JobDetail creditCheckJob = JobBuilder.newJob(CreditCheckTimer.class)
                .withIdentity(creditCheck, checkGroup)
                .build();
        Trigger creditCheckTrigger = TriggerBuilder.newTrigger()
                .withIdentity(creditCheck, checkGroup)
                .withSchedule(scheduleBuilder)
                .build();
        JobKey.jobKey(creditCheck, checkGroup);
        scheduler.scheduleJob(creditCheckJob, creditCheckTrigger);
    }

    /**
     * 创建定时检查逾期任务
     * @throws Exception
     */
    public void buildOverdueCheck() throws Exception {
        String overdueCheck = "overdueCheck";
        String checkGroup = "checkGroup";
        if (scheduler.checkExists(JobKey.jobKey(overdueCheck, checkGroup))) {
            return;
        }
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.dailyAtHourAndMinute(2,0);
        JobDetail overdueCheckJob = JobBuilder.newJob(OverdueCheckTimer.class)
                .withIdentity(overdueCheck, checkGroup)
                .build();
        Trigger overdueCheckTrigger = TriggerBuilder.newTrigger()
                .withIdentity(overdueCheck, checkGroup)
                .withSchedule(scheduleBuilder)
                .build();

        scheduler.scheduleJob(overdueCheckJob, overdueCheckTrigger);
    }

    /**
     * 创建定时放款任务
     * @throws Exception
     */
    public void buildMakeLoan() throws Exception {
        String makeLoan = "makeLoan";
        String loanGroup = "loanGroup";
        if (scheduler.checkExists(JobKey.jobKey(makeLoan, loanGroup))) {
            return;
        }
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.repeatMinutelyForever(1000);
        JobDetail makeLoanJob = JobBuilder.newJob(OverdueCheckTimer.class)
                .withIdentity(makeLoan, loanGroup)
                .build();
        Trigger makeLoanTrigger = TriggerBuilder.newTrigger()
                .withIdentity(makeLoan, loanGroup)
                .withSchedule(scheduleBuilder)
                .build();

        scheduler.scheduleJob(makeLoanJob, makeLoanTrigger);
    }




}
